package com.ke.rule.base;


import java.util.stream.Stream;

public enum FuncUnitType {
  Validator("FuncUnitType.validator",   0x0001, IValidator.class),
  Processor("FuncUnitType.processor",   0x0010, IProcessor.class),
  Dispatcher("FuncUnitType.dispatcher", 0x0100, IDispatcher.class);
  
  final String id;
  final int value;
  final Class<?> clazz;
  
  private FuncUnitType(String id, int value, Class<?> clazz) {
    this.id = id;
    this.value = value;
    this.clazz = clazz;
  }
  
  /**
   * id of
   * @param id
   * @return
   */
  public static FuncUnitType idOf(String id) {
    return Stream.of(FuncUnitType.values()).filter(type -> type.id.equals(id)).findFirst().orElse(null);
  }
  
  static final int V = Validator.value;
  static final int P_D = Processor.value | Dispatcher.value;
  static final int V_P_D = Validator.value | Processor.value | Dispatcher.value;
  static final int D = Dispatcher.value;
  
  public static FuncUnitType of(Class<? extends IFuncUnit> clazz) {
    if (clazz == null) {
      return null;
    }
    for (FuncUnitType type : FuncUnitType.values()) {
      if (type.clazz.isAssignableFrom(clazz)) {
        return type;
      }
    }
    throw new IllegalArgumentException("Unexpected FuncUnit class: " + clazz);
  }
}
